from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from google.appengine.api import users, memcache
from datetime import datetime
import time
import random
import pylast
import gdata.youtube
import gdata.youtube.service

def cb(response):
	response["Expires"] = 'Thu, 19 Nov 1981 08:52:00 GMT'
	response["Last-Modified"] = str(datetime.now().ctime()) +" GMT"
	response["Cache-Control"] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0, must-revalidate'
	response["Pragma"] = 'no-cache'
	response["P3P"] = 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'
	return response
		
def index(request):
	
	if request.method == 'POST':
		API_KEY = ""
		API_SECRET = ""
		username = ""
		password_hash = pylast.md5("")
		network = pylast.get_lastfm_network(api_key = API_KEY, api_secret = API_SECRET, username = username, password_hash = password_hash)

		s = str(request.POST['text'])
		
		if not memcache.get(s + 'tops'):
			top_songs = []
			artist = network.get_artist(s)
		
			top_tracks = artist.get_top_tracks()
			for i in range(0,7):
				top_songs.append(top_tracks[i]['item'])
			
			tops = artist.get_similar(10)
			memcache.add(s + 'top_similar', tops, 7200)
	
			artist1 = network.get_artist(str(tops[0]['item']))
			top_tracks = artist1.get_top_tracks()
			for i in range(0,10):
				top_songs.append(top_tracks[i]['item'])
			
			artist2 = network.get_artist(str(tops[1]['item']))
			top_tracks = artist2.get_top_tracks()
			for i in range(0,4):
				top_songs.append(top_tracks[i]['item'])
			 
			artist3 = network.get_artist(str(tops[2]['item']))
			top_tracks = artist3.get_top_tracks()
			for i in range(0,4):
				top_songs.append(top_tracks[i]['item'])
				
			artist4 = network.get_artist(str(tops[3]['item']))
			top_tracks = artist4.get_top_tracks()
			for i in range(0,4):
				top_songs.append(top_tracks[i]['item'])
				
			artist5 = network.get_artist(str(tops[4]['item']))
			top_tracks = artist5.get_top_tracks()
			for i in range(0,2):
				top_songs.append(top_tracks[i]['item'])
			
			artist6 = network.get_artist(str(tops[5]['item']))
			top_tracks = artist6.get_top_tracks()
			for i in range(0,2):
				top_songs.append(top_tracks[i]['item'])
			
			artist7 = network.get_artist(str(tops[6]['item']))
			top_tracks = artist7.get_top_tracks()
			for i in range(0,2):
				top_songs.append(top_tracks[i]['item'])
			
			memcache.add(s + 'tops', top_songs, 7200)
		
		top_songs = memcache.get(s + 'tops')
		similar = memcache.get(s + 'top_similar')

		random.shuffle(top_songs)
		
		#google youtubeApi
		client = gdata.youtube.service.YouTubeService()
		query = gdata.youtube.service.YouTubeVideoQuery()
		
		query.vq = str(top_songs[0])
		query.max_results = '1'
		query.start_index = '1'
		query.racy = 'exclude'
		query.format = '5'
		query.orderby = 'relevance'
		
		video = client.YouTubeQuery(query)
		
		title = video.entry[0].title.text
		url = video.entry[0].media.content[0].url

		response = render_to_response('radio.html', {'search': s, 'title': title, 'url': url, 'similar': similar })
		return cb(response)
	
	response = render_to_response('search.html')
	return cb(response)

def test(request):
	response = render_to_response('test.html')
	return cb(response)